package com.stationbuilding.fragment.message;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.TestData;
import com.stationbuilding.activity.ShowActivity;
import com.stationbuilding.adapter.message.TaskListAdapter;
import com.stationbuilding.api.RetrofitManager;
import com.stationbuilding.bean.ProjectInfoBean;
import com.stationbuilding.bean.QuestionFlowBean;
import com.stationbuilding.bean.QuestionFlowListBean;
import com.stationbuilding.bean.TaskBean;
import com.stationbuilding.utils.DialogUtils;
import com.stationbuilding.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class TaskFragment extends Fragment {

    private View view;
    private ListView listView;
    private TaskListAdapter taskListAdapter;
    private List<QuestionFlowBean> taskList = new ArrayList<>();
    private String token = PreferenceHelper.getInstance(getActivity()).getToken();
    private Dialog dialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_task, container, false);
        initViews();
        initEvents();
        return view;
    }

    private void initViews() {
        listView = (ListView) view.findViewById(R.id.task_list);
        setAdapter();
    }

    private void setAdapter() {
        taskListAdapter = new TaskListAdapter(getActivity(), taskList);
        listView.setAdapter(taskListAdapter);
        // 数据加载
        loadTaskData();
    }

    private void initEvents() {
        // ListView的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ShowActivity.class);
                intent.putExtra("id", taskList.get(position).getId());
                intent.putExtra("questionId", taskList.get(position).getQuestionId());
                intent.putExtra("status", taskList.get(position).getStatus());
                startActivity(intent);
            }
        });
    }

    private void loadTaskData() {
        // 获取站房ID
        String projectId = PreferenceHelper.getInstance(getActivity()).getProjectId();
        // 获取用户ID
        String userId = PreferenceHelper.getInstance(getActivity()).getUserId();
        RetrofitManager.toSubscribe(RetrofitManager.getApiService().getQuestionFlowListByProjectIdAndUserId(token, projectId, userId), new Observer<QuestionFlowListBean>() {
            @Override
            public void onComplete() {
                DialogUtils.closeDialog(dialog);
            }
            @Override
            public void onError(Throwable e) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                DialogUtils.closeDialog(dialog);
            }
            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
                dialog = DialogUtils.createLoadingDialog(getActivity(), "数据加载中...");
            }
            @Override
            public void onNext(QuestionFlowListBean questionFlowListBean) {
                if (questionFlowListBean.getCode() != 0) {
                    Toast.makeText(getActivity(), "获取站房信息失败:" + questionFlowListBean.getCode(), Toast.LENGTH_SHORT).show();
                } else {
                    taskList = questionFlowListBean.getQuestionFlowEntityList();
                    taskListAdapter.updateListView(taskList);
                }
            }
        });
    }
}
